package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.AssesRuleMapper;
import com.ruoyi.system.service.*;
import liquibase.pro.packaged.S;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.IntStream;

/**
 * 考核规则Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class AssesRuleServiceImpl implements IAssesRuleService
{
    @Autowired
    private AssesRuleMapper assesRuleMapper;
    @Autowired
    private IAssesRuleSystemService assesRuleSystemService;
    @Autowired
    private IAssesRuleArtificialService assesRuleArtificialService;

    /**
     * 查询考核规则
     *
     * @param id 考核规则主键
     * @return 考核规则
     */
    @Override
    public AssesRule selectAssesRuleById(Long id)
    {
        AssesRule assesRule = assesRuleMapper.selectAssesRuleById(id);
        AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
        assesRuleSystem.setAssesRuleId(id);
        List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
        AssesRuleArtificial assesRuleArtificial = new AssesRuleArtificial();
        assesRuleArtificial.setAssesRuleId(id);
        List<AssesRuleArtificial> assesRuleArtificials = assesRuleArtificialService.selectAssesRuleArtificialList(assesRuleArtificial);
        assesRule.setAssesRuleSystemList(assesRuleSystems);
        assesRule.setAssesRuleArtificialList(assesRuleArtificials);
        return assesRule;
    }

    /**
     * 查询考核规则
     *
     * @param assesRule 考核规则主键
     * @return 考核规则
     */
    @Override
    public AssesRule selectAssesRuleByAssesRule(AssesRule assesRule){
        return assesRuleMapper.selectAssesRuleByAssesRule(assesRule);
    }

    /**
     * 查询考核规则列表
     *
     * @param assesRule 考核规则
     * @return 考核规则
     */
    @Override
    public List<AssesRule> selectAssesRuleList(AssesRule assesRule)
    {
        List<AssesRule> assesRules = assesRuleMapper.selectAssesRuleList(assesRule);
        IntStream.range(0,assesRules.size()).forEach(
                i->{
                   int index = i+1;
                    AssesRule assesRuleVo = assesRules.get(i);
                    assesRuleVo.setOrderNum(index);
                });
        return assesRules;
    }

    /**
     * 新增考核规则
     *
     * @param assesRule 考核规则
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertAssesRule(AssesRule assesRule)
    {
        assesRule.setCreateTime(DateUtils.getNowDate());
        String effectiveTerm = assesRule.getEffectiveTerm();
        String deptType = assesRule.getDeptType();


        //添加当前年份之后的规则 状态待生效并只能有一条
        Integer nowYear = Integer.valueOf(DateUtils.getNowYear());
        if(Integer.valueOf(effectiveTerm)>nowYear){
            AssesRule assesRuleVo = new AssesRule();
            assesRuleVo.setStatus("2");
            assesRuleVo.setEffectiveTerm(effectiveTerm);
            assesRuleVo.setDeptType(deptType);
            AssesRule assesRule1 = assesRuleMapper.selectAssesRuleByAssesRule(assesRuleVo);
            if(!ObjectUtils.isEmpty(assesRule1)){
                return AjaxResult.error("该组织类型已经有"+effectiveTerm+"待生效的规则,请勿重复添加");
            }
            assesRule.setStatus("2");
        }
        if(Integer.valueOf(effectiveTerm)<nowYear){
            AssesRule assesRuleVo = new AssesRule();
            assesRuleVo.setStatus("1");
            assesRuleVo.setEffectiveTerm(effectiveTerm);
            assesRuleVo.setDeptType(deptType);
            AssesRule assesRule1 = assesRuleMapper.selectAssesRuleByAssesRule(assesRuleVo);
            if(!ObjectUtils.isEmpty(assesRule1)){
                return AjaxResult.error("该组织类型已经有"+effectiveTerm+"过期的规则,请勿重复添加");
            }
            assesRule.setStatus("1");
        }
        AssesRule assesRuleSelect = new AssesRule();
        assesRuleSelect.setEffectiveTerm(effectiveTerm);
        assesRuleSelect.setDeptType(deptType);
        AssesRule assesRuleByEffectiveTerm = assesRuleMapper.selectAssesRuleByAssesRule(assesRuleSelect);
        if(!ObjectUtils.isEmpty(assesRuleByEffectiveTerm)){
            return AjaxResult.error("该组织类型已经有"+effectiveTerm+"有效期内的规则,请勿重复添加");
        }
        try {
            assesRuleMapper.insertAssesRule(assesRule);
            List<AssesRuleArtificial> assesRuleArtificialList = assesRule.getAssesRuleArtificialList();
            if(!CollectionUtils.isEmpty(assesRuleArtificialList)){
                for(AssesRuleArtificial ruleArtificial : assesRuleArtificialList){
                    ruleArtificial.setAssesRuleId(assesRule.getId());
                    assesRuleArtificialService.insertAssesRuleArtificial(ruleArtificial);
                }
            }
            List<AssesRuleSystem> assesRuleSystemList = assesRule.getAssesRuleSystemList();
            if(!CollectionUtils.isEmpty(assesRuleSystemList)){
                for(AssesRuleSystem ruleSystem: assesRuleSystemList){
                    ruleSystem.setAssesRuleId(assesRule.getId());
                    assesRuleSystemService.insertAssesRuleSystem(ruleSystem);
                }
            }
            return AjaxResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("添加失败");
        }
    }

    /**
     * 修改考核规则
     *
     * @param assesRule 考核规则
     * @return 结果
     */
    @Override
    public AjaxResult updateAssesRule(AssesRule assesRule)
    {
        assesRule.setUpdateTime(DateUtils.getNowDate());
        String effectiveTerm = assesRule.getEffectiveTerm();
        String deptType = assesRule.getDeptType();
        if(StringUtils.isNotBlank(effectiveTerm)){
            //添加当前年份之后的规则 状态待生效并只能有一条
            Integer nowYear = Integer.valueOf(DateUtils.getNowYear());
            if(Integer.valueOf(effectiveTerm)>nowYear){
                AssesRule assesRuleVo = new AssesRule();
                assesRuleVo.setStatus("2");
                assesRuleVo.setEffectiveTerm(effectiveTerm);
                AssesRule assesRule1 = assesRuleMapper.selectAssesRuleByAssesRule(assesRuleVo);
                if(!ObjectUtils.isEmpty(assesRule1)){
                    return AjaxResult.error("该组织类型已经有已经有"+effectiveTerm+"待生效的规则,请勿重复添加");
                }
                assesRule.setStatus("2");
            }
            if(Integer.valueOf(effectiveTerm)<nowYear){
                AssesRule assesRuleVo = new AssesRule();
                assesRuleVo.setStatus("1");
                assesRuleVo.setEffectiveTerm(effectiveTerm);
                AssesRule assesRule1 = assesRuleMapper.selectAssesRuleByAssesRule(assesRuleVo);
                if(!ObjectUtils.isEmpty(assesRule1)){
                    return AjaxResult.error("该组织类型已经有已经有"+effectiveTerm+"过期的规则,请勿重复添加");
                }
                assesRule.setStatus("1");
            }
            AssesRule assesRuleSelect = new AssesRule();
            assesRuleSelect.setEffectiveTerm(effectiveTerm);
            assesRuleSelect.setDeptType(deptType);
            AssesRule assesRuleByEffectiveTerm = assesRuleMapper.selectAssesRuleByAssesRule(assesRuleSelect);
            AssesRule assesRuleById = assesRuleMapper.selectAssesRuleById(assesRule.getId());
            if(!ObjectUtils.isEmpty(assesRuleByEffectiveTerm)&&!effectiveTerm.equals(assesRuleById.getEffectiveTerm())){
                return AjaxResult.error("该组织类型已经有"+effectiveTerm+"有效期内的规则,请勿重复添加");
            }
        }
        try {
            assesRuleMapper.updateAssesRule(assesRule);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }


    /**
     * 删除考核规则信息
     *
     * @param id 考核规则主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAssesRuleById(Long id)
    {
        int i = assesRuleMapper.deleteAssesRuleById(id);
        AssesRuleSystem assesRuleSystem = new AssesRuleSystem();
        assesRuleSystem.setAssesRuleId(id);
        List<AssesRuleSystem> assesRuleSystems = assesRuleSystemService.selectAssesRuleSystemList(assesRuleSystem);
        for(AssesRuleSystem ruleSystem : assesRuleSystems){
            assesRuleSystemService.deleteAssesRuleSystemById(ruleSystem.getId());
        }
        AssesRuleArtificial assesRuleArtificial = new AssesRuleArtificial();
        assesRuleArtificial.setAssesRuleId(id);
        List<AssesRuleArtificial> assesRuleArtificials = assesRuleArtificialService.selectAssesRuleArtificialList(assesRuleArtificial);
        for(AssesRuleArtificial ruleArtificial : assesRuleArtificials){
            assesRuleArtificialService.deleteAssesRuleArtificialById(ruleArtificial.getId());
        }
        return i;
    }
}
